<?php

namespace app\controllers;

use yii\web\Controller;

use app\models\InsurancePolicy;
use app\models\InsurancePolicyHolder;
use app\models\InsurancePolicyInsured;
use app\services\ToolServices;

class UserController extends BaseController
{

    public function init()
    {
        parent::init();
        $this->layout = "insurance-buy";
    }

    public function actionWechat()
    {
        $user_id = $this->user_id;
        $token = ToolServices::encrypt($user_id);
        header(sprintf('Location: http://web.funfind.cn/details?token=%s', $token));
        exit();
    }

    //邀请
    public function actionInvite()
    {
        $user_id = $this->user_id;//用户ff_id
        $wechatId = trim($this->request->get('wechatId', ' '));//邀请人ff_id
        if (empty($wechatId)) exit;
        $res = file_get_contents("http://api.funfind.cn/invite/invite?userId=$user_id&wechatId=$wechatId");//调用接口
        $res = json_decode($res);
        if ($res) {
            header('Location:http://m.funfind.cn/index/list?code=FFAXLX_FFLXWY');//跳转到指定页面
        }
        exit;

    }

    /**
     * 我的订单
     */
    public function actionMyOrder()
    {
        $user_id = $this->user_id;
        $order_status = $this->request->get('type', 0);

        $order_info = InsurancePolicy::getInsurancePolicy($user_id, $order_status);

        $order = [];
        if ($order_info) {
            foreach ($order_info as $item) {
                $order[] = array(
                    "id" => $item["id"],
                    "title" => $item["project_name"],
                    "name" => empty($item["policy_holder_name"]) ? "未添加" : $item["policy_holder_name"],
                    "plan_code" => $item["plan_code"],
                    "premium_total" => $item["premium_total"],
                );
            }
        }
        return $this->render('my-order', [
            "order" => $order,
            "type" => $order_status
        ]);
    }

    public function actionOrderDetail()
    {
        $user_id = $this->user_id;
        $order_id = $this->request->get('id', 0);

        $policy_info = InsurancePolicy::find()->where(["account_id" => $user_id, "id" => $order_id])->limit(1)->one();
        $data = [];
        $err_info = 0;
        if ($policy_info) {
            if (!$policy_info->policy_id) {
                $err_info = 1;
            } else {
                $data = [
                    "plan_code" => $policy_info->plan_code,
                    "title" => $policy_info->project_name,
                    "policy_id" => $policy_info->policy_id,
                    "effect_date" => $policy_info->effect_date,
                    "expire_date" => $policy_info->expire_date,
                    "premium_total" => $policy_info->premium_total
                ];
                // 被保险人,多个
                $policy_insure = InsurancePolicyInsured::find()->where(["policy_id" => $order_id])->asArray()->all();
                if ($policy_insure) {
                    $policy_insuce_info = [];
                    foreach ($policy_insure as $item) {
                        $policy_insuce_info[] = [
                            "insuce_name" => $item["insured_name"],
                            "insuce_type" => InsurancePolicyHolder::$userIdCard[$item["id_type"]],
                            "insuce_number" => $item["id_number"],
                        ];
                    }
                    $data["policy_insuce"] = $policy_insuce_info;
                } else {
                    $err_info = 1;
                }
                // 投保人
                $policy_holder = InsurancePolicyHolder::find()->where(["policy_id" => $order_id])->limit(1)->one();
                if ($policy_holder) {
                    $data["holder_name"] = $policy_holder->policy_holder_name;
                    $data["holder_phone"] = $policy_holder->telephone;
                    $data["holder_email"] = $policy_holder->email;
                } else {
                    $err_info = 1;
                }
            }
        } else {
            $err_info = 1;
        }

        return $this->render('order-detail', [
            "data" => $data,
            "err_info" => $err_info,
        ]);
    }
}